package com.xh.jf_blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xh.jf_blog.bean.Article;
import com.xh.jf_blog.bean.ArticleLike;
import com.xh.jf_blog.bean.Userinfo;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.mapper.ArticleLikeMapper;
import com.xh.jf_blog.service.ArticleLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/2/22/022
 */
@Service
@Transactional
public class ArticleLikeServiceImpl implements ArticleLikeService {
    @Autowired
    private ArticleLikeMapper articleLikeMapper;

    public void save(ArticleLike articleLike) {
        if (articleLikeMapper.getAll(articleLike).isEmpty()) {
            articleLikeMapper.insertSelective(articleLike);
        } else {
            articleLikeMapper.deleteLike(articleLike);
        }
    }

    @Override
    public R getLikeListByArticleId(int page, int limit, int articleId) {
        PageHelper.startPage(page, limit);
        List<Userinfo> list = articleLikeMapper.getLikeListByArticleId(articleId);
        PageInfo<Userinfo> pageInfo = new PageInfo<>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R getCollectionListByArticleId(int page, int limit, int articleId) {
        PageHelper.startPage(page, limit);
        List<Userinfo> list = articleLikeMapper.getCollectionListByArticleId(articleId);
        PageInfo<Userinfo> pageInfo = new PageInfo<>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R getLikeListByUserinfoId(int page, int limit, int userinfoId) {
        PageHelper.startPage(page, limit);
        List<Article> list = articleLikeMapper.getLikedListByUserinfoId(userinfoId);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R getCollectionListByUserinfoId(int page, int limit, int userinfoId) {
        PageHelper.startPage(page, limit);
        List<Article> list = articleLikeMapper.getCollectionedListByUserinfoId(userinfoId);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R getFollowListByUserinfoId(int page, int limit, int userinfoId) {
        PageHelper.startPage(page, limit);
        List<Userinfo> list = articleLikeMapper.getFollowListByUserinfoId(userinfoId);
        PageInfo<Userinfo> pageInfo = new PageInfo<>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R getFollowedListByUserinfoId(int page, int limit, int userinfoId) {
        PageHelper.startPage(page, limit);
        List<Userinfo> list = articleLikeMapper.getFollowedListByUserinfoId(userinfoId);
        PageInfo<Userinfo> pageInfo = new PageInfo<>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }
}
